package FilePanel.controls;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import FilePanel.Control;
import FilePanel.FilePanelControl;
import FilePanel.FilePanelModel;
import FilePanel.FilePanelUI;

/**
 * @author      Erwan Osouf, Lucas Ennouchi
 * @version     1.0                                
 * @since       29/01/2012
 */

public class ControlField extends Control implements CaretListener,Observer{
	
	private JTextField field;
	
	/**
	 * Constructor
	 * @param field 
	 */
	public ControlField(JTextField field) {
		super();
		this.field = field;
	}
	
	
	@Override
	/**
	 * updates the caret
	 * @param e - caret event
	 */
	public void caretUpdate(CaretEvent e) {
		String path = field.getText();
		getSuperController().buttonEnabled(check(path));		
	}


	@Override
	/**
	 * update
	 * @parm o - FilePanelModel
	 * @param arg - 
	 */
	public void update(Observable o, Object arg) {
		FilePanelModel model = (FilePanelModel)o;
		field.setText(model.getRoot().getPath());
		
	}
	
	/**
	 * Checks the string if it is a directory
	 * @param t the string
	 * @return boolean if it is a directory
	 */
	private boolean check(String t){
		File test = new File(t);
		return test.exists()&&test.isDirectory();
	}

/**
 * set the field with the path
 * @param path - The Path
 */
	public void setText(String path) {
		field.setText(path);
		
	}


	

}
